//
//  HTDriverMainVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/31.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTDriverMainVC.h"
#import "HTDriverMainCell.h"
#import "HTGoodsSearchCell.h"
#import "HTDriverLocationVC.h"
#import "HTDriverMainModel.h"
#import "UIImageView+WebCache.h"
@interface HTDriverMainVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableViewFirst;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameIconLabel;
@property (weak, nonatomic) IBOutlet UILabel *caridLabel;

@property (weak, nonatomic) IBOutlet UIImageView *caridImageView;
@property (weak, nonatomic) IBOutlet UILabel *truckLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;
@property (weak, nonatomic) IBOutlet UIImageView *mapImageView;
@property (weak, nonatomic) IBOutlet UILabel *mapLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowNextImageView;
@property (weak, nonatomic) IBOutlet UIButton *mapButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *middleHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (nonatomic,strong) HTDriverMainModel *driverMainModel;

@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UIView *tableAreaView;

//数据处理

@property (nonatomic,strong) NSMutableArray *dataSource;



@end

@implementation HTDriverMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 30;
    [self configTableView];
    [self reloadInitData];
    [self netLoadData];
}
- (void)reloadInitData{
    if (self.dataSource.count <= 0) {
        self.tableViewFirst.hidden = YES;
        self.middleHeight.constant = 90;
    }else{
        self.tableViewFirst.hidden = NO;
        self.middleHeight.constant = 90 * self.dataSource.count;
    }
    [self.view layoutIfNeeded];
    [self.tableViewFirst reloadData];
    if (self.tableAreaView.top + 60 + self.middleHeight.constant + self.bottomHeight.constant < kHTScreenHeight ) {
        self.scrollHeight.constant = kHTScreenHeight - self.bottomHeight.constant;
    }else{
        self.scrollHeight.constant = self.tableAreaView.top + self.middleHeight.constant + self.bottomHeight.constant;
    }
    [self.view layoutIfNeeded];
}
- (void)configTableView{
    self.title = @"司机主页";
    self.tableViewFirst.delegate = self;
    self.tableViewFirst.dataSource = self;
    [self.tableViewFirst registerNib:[UINib nibWithNibName:@"HTDriverMainCell" bundle:nil] forCellReuseIdentifier:DriverMainCell];
}
- (IBAction)didClickMap:(id)sender {
    if (self.driverMainModel == nil || self.driverMainModel.carid == nil) {
        return;
    }
    HTDriverLocationVC *driver = [[HTDriverLocationVC alloc]init];
    driver.carid = self.driverMainModel.carid;
    [self.navigationController pushViewController:driver animated:YES];
}

#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableViewFirst) {
        return self.dataSource.count;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableViewFirst) {
        return 90;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableViewFirst) {
        HTDriverMainCell *cell = [tableView dequeueReusableCellWithIdentifier:DriverMainCell forIndexPath:indexPath];
        cell.driverMainCellModel = self.dataSource[indexPath.row];
        return cell;
    }
    return nil;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

#pragma mark --- 网络请求
- (void)netLoadData{
    [self startHud];
    [HTHttpRequestManager requestTruckDriverInfoWithDriverid:self.ID success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"司机信息：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"司机信息请求失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        self.driverMainModel = [HTDriverMainModel mj_objectWithKeyValues:responseObject];
        self.dataSource = [HTDriverMainCellModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
        [self reloadInitData];
        
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"司机信息请求失败"];
    }];
}

- (void)setDriverMainModel:(HTDriverMainModel *)driverMainModel{
    _driverMainModel = driverMainModel;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:driverMainModel.picurl] placeholderImage:[UIImage imageNamed:@"bg_headsculpture"]options:SDWebImageRefreshCached];
    self.nameLabel.text = driverMainModel.dirvername;
    if ([driverMainModel.ismastr isEqualToString:@"0"]) {
        //主驾
        self.nameIconLabel.text = @"[主驾]";
    }else{
        //副驾
        self.nameIconLabel.text = @"[副驾]";
    }
    if ([driverMainModel.numbertype isEqualToString:@"1"]) {
        //蓝牌
        self.caridImageView.image = [UIImage imageNamed:@"bg_license_blue"];
       
    }else{
        //黄牌
        self.caridImageView.image = [UIImage imageNamed:@"bg_license_yellow"];
    }
    NSString *carid = driverMainModel.numbers;
    if (driverMainModel.numbers != nil || driverMainModel.numbers.length > 2) {
        carid = [carid stringByReplacingCharactersInRange:NSMakeRange(2, 0) withString:@"·"];
    }
    self.caridLabel.text = carid;
    self.truckLabel.text = [NSString stringWithFormat:@"%@/%@米/%@吨",driverMainModel.types,driverMainModel.length,driverMainModel.loads];
    self.leftLabel.text = [NSString stringWithFormat:@"交易单数 %@单",driverMainModel.counts];
    self.rightLabel.text = [NSString stringWithFormat:@"交易里程 %@公里",driverMainModel.learver];
    if ([driverMainModel.state isEqualToString:@"1"]) {
        //运输中
        self.mapLabel.text = @"该司机目前正在运输中";
        self.mapImageView.image = [UIImage imageNamed:@"map_empty"];
    }else{
        //停止
        self.mapLabel.text = @"该司机目前空车";
        self.mapImageView.image = [UIImage imageNamed:@"map_trans"];
    }
   
    if ([driverMainModel.isoffer isEqualToString: [NSString stringWithFormat:@"%d",YES]]) {
        //是熟车
        [self.firstButton setTitle:@"取消熟车" forState:UIControlStateNormal];
    }else{
        //不是熟车
        [self.firstButton setTitle:@"添加熟车" forState:UIControlStateNormal];
    }
   
}
- (IBAction)didClickFirstButton:(UIButton *)sender {
    if (self.driverMainModel == nil || self.driverMainModel.carid == nil || self.driverMainModel.carid.length <= 0) {
        [HTShowUtil showMessage:@"司机信息请求失败"];
        return;
    }
    if ([sender.titleLabel.text isEqualToString:@"取消熟车"]) {
        [self netLoadDataDeleteTruck];
    }else{
        [self netLoadDataAddTruck];
    }
}
- (IBAction)didClickSecondButton:(id)sender {
    if (self.driverMainModel == nil || self.driverMainModel.phone == nil || self.driverMainModel.phone.length <= 0) {
        [HTShowUtil showMessage:@"司机信息请求失败"];
        return;
    }
    NSString *phone = self.driverMainModel.phone;
    NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
- (void)netLoadDataAddTruck{
    [self startHud];
    [HTHttpRequestManager requestTruckAddWithCarid:self.driverMainModel.carid success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"添加熟车：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"添加熟车失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
         [self.firstButton setTitle:@"取消熟车" forState:UIControlStateNormal];
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"添加熟车失败"];
    }];
}
- (void)netLoadDataDeleteTruck{
    [self startHud];
    [HTHttpRequestManager requestTruckDeleteWithCarid:self.driverMainModel.carid success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"删除熟车：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"删除熟车失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        [self.firstButton setTitle:@"添加熟车" forState:UIControlStateNormal];
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"删除熟车失败"];
    }];
}
@end
